/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rie.test;

import com.rie.dao.ReportDAO;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import rie.model.Processus;
import rie.model.SystemeInfo;

/**
 *
 * @author Michael
 */
@WebServlet(name = "controleurReport", urlPatterns = {"/controleurReport"})
public class ControleurReport extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet displayListClient</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet displayListClient at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        PrintWriter out = response.getWriter();
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Historique</title>");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Report</h1>");
            
            
            if(request.getParameter("id")!=null){
           try{
            ReportDAO reportDao = new ReportDAO();
            SystemeInfo si = reportDao.getReport(Integer.valueOf(request.getParameter("id")));          
           
                out.println("Serveur : " + si.getServeur() + ".<br/>");
                if(si.getCommentaire()!=null){
                    out.println("Commentaire : " + si.getCommentaire()+ "<br/>");
                }
                if (si.getCpu().getLoadCPU()>0) {
                    out.println("Sa charge CPU est de " + si.getCpu().getLoadCPU() + "%.<br/>");
                }
                if (si.getMemoire().getFreeRam()>0) {
                    out.println("Sa mémoire RAM libre est de " + si.getMemoire().getFreeRam() +"Mo.<br/>");
                }
                if (si.getListProcessus()!=null && !si.getListProcessus().isEmpty()) {
                    out.println("<h2>Les processus courants</h2>");
                    out.println("<table>");
                    out.println("<tr><th>Nom</th><th>PID</th><th>Mémoire</th></tr>");
                    for(Processus p : si.getListProcessus()){
                        out.println("<tr><td>"+p.getName()+"</td><td>"+p.getPid()+"</td><td>"+p.getMemoryUsage()+"</td></tr>");
                    }
                    out.println("</table>");
                }
            } catch (NumberFormatException e) {
               out.println("L'identifiant du report est incorrect.<br/>");  
            }
            }else{
            out.println("Aucun résultat à votre recherche.<br/>");   
            }
            out.println("<a href='./index.jsp'>Retour à la page de recherche</a>"); 
            out.println("</body>");
            out.println("</html>");
            out.close();      
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
